//1859. 将句子排序
//思路：哈希表的应用

#include <stdlib.h>
#include <string.h>
char * sortSentence(char * s){
    int len = strlen(s);
    char* hash[10] = {NULL};

    char* tail = s;
    int pos = 0;
    int i = 0;
    while(s[i])
    {
        if(s[i] == ' ')
        {
            s[i] = '\0';
            pos = s[i-1]- '0';
            s[i-1] = ' ';
            hash[pos] = tail;
            tail = &s[i+1];
        }
        i++;
    }
    pos = s[i-1] - '0';
    s[i-1] = ' ';
    hash[pos] = tail;
    char* res = (char*)malloc(sizeof(char)*(len+1));
    memset(res, '\0', len+1);
    if(NULL == res)
    {
        perror("malloc fail:");
        return NULL;
    }
    
    for(i = 0; i < 10; i++)
    {
        if(hash[i])
        {
            strcat(res, hash[i]);
        }
    }
    int rlen = strlen(res);
    res[rlen-1] = '\0';

    return res;
}